<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="mainwidth" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

          <!--任务类别   -->
          <el-form-item label="抄送状态" prop="status">
            <el-select
              v-model="queryParams.cc_state"
              placeholder="抄送状态"
              clearable
              size="small"
              style="width: 110px"
            >
              <el-option
                v-for="dict in CCstatus"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>

          <!--抄送结果   -->
          <el-form-item label="抄送结果" prop="status">
            <el-select
              v-model="queryParams.cc_result"
              placeholder="抄送结果"
              clearable
              size="small"
              style="width: 110px"
            >
              <el-option
                v-for="dict in Cchis_result"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>

          <!--  时间类型-->
          <el-form-item label="时间类型" prop="status">
            <el-select
              v-model="queryParams.timetype"
              placeholder="时间类型"
              clearable
              size="small"
              style="width: 110px"
            >
              <el-option
                v-for="dict in timetypeHis"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="selTime"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              style="width: 360px"
              align="right">
            </el-date-picker>

          </el-form-item>

        </el-form>

        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" style="margin-bottom: 10px">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5" style="margin-bottom:-10px;line-height:35px">
              <el-input
                v-model="queryParams.value"
                placeholder="查询值"
                clearable
                size="small"
                style="width: 350px"
                @keyup.enter.native="handleQuery"
              >
                <el-select
                  v-model="queryParams.type"
                  placeholder="查询条件"
                  clearable
                  slot="prepend"
                  style="width: 110px"
                >
                  <el-option
                    v-for="dict in typeOptionsCcUrl"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-input>

              <el-form-item label="建筑物选择" label-width="100px" style="margin-bottom:10px;line-height:35px">
                <el-select
                  v-model="queryParams.name_id"
                  placeholder="请选择建筑物"
                  clearable
                  size="small"
                  style="width: 150px"
                >
                  <el-option
                    v-for="dict in Name_result"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
                </el-select>
              </el-form-item>
            </el-col>


            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"

              >新增
              </el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"

              >导出
              </el-button>
            </el-col>

            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>
        </el-form>

        <el-table v-loading="loading" :data="cardList" @selection-change="handleSelectionChange">
          <el-table-column label="序号" align="center" prop="id" v-if="columns[0].visible"/>
          <el-table-column label="二维码" align="center" prop="qrcode" v-if="columns[1].visible"/>
          <el-table-column label="安装点名称" align="center" prop="install_name" v-if="columns[2].visible"/>
          <el-table-column label="类型-型号" align="center" prop="type_model" v-if="columns[3].visible"/>
          <el-table-column label="建筑物" align="center" prop="name_id" v-if="columns[4].visible">
            <template slot-scope="scope">
              {{ tools.getDkeyValue(Name_result, scope.row.name_id) }}
            </template>
          </el-table-column>
          <el-table-column label="楼层" align="center" prop="y_floor" v-if="columns[5].visible"/>
          <el-table-column label="公司所属" align="center" prop="dept_id" v-if="columns[6].visible"/>
          <el-table-column label="IMEI" align="center" prop="imei" v-if="columns[7].visible"/>
          <el-table-column label="现场图片" align="center" prop="picture" v-if="columns[8].visible"/>
          <el-table-column label="已关联数" align="center" prop="yassociated" v-if="columns[9].visible"/>
          <el-table-column label="被关联数" align="center" prop="bassociated" v-if="columns[10].visible"/>
          <el-table-column label="安装人" align="center" prop="Instalr" v-if="columns[12].visible"/>
          <el-table-column label="安装时间" align="center" prop="create_time" v-if="columns[12].visible"/>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-dropdown >
                <el-button type="primary"  size="small" class-name="small-padding fixed-width">
                  操作<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item     @click.native="Relation(scope.row)">关联设备</el-dropdown-item>
                  <el-dropdown-item     @click.native="Unbind(scope.row)">解绑安装点</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!--新增-->
    <el-dialog title="绑定安装点" :visible.sync="open" width="800px" append-to-body>

      <el-form :model="form">
        <el-form-item label="IMEI" :required="true" style="margin-left: 50px">
          <div class="demo-autocomplete">
            <el-autocomplete
              class="inline-input"
              v-model="form.imei"
              :fetch-suggestions="querySearch"
              placeholder="请输入"
              style="width: 330px"
              :trigger-on-focus="false"
              @select="handleSelect"
            ></el-autocomplete>
          </div>
        </el-form-item>
        <br>
        <el-form-item label="单位" :required="true" style="margin-left: 50px">
          <el-input placeholder="请输入" v-model="form.dept_id" style="width: 330px"/>
          <el-button type="primary" style="margin-left: 10px" size="small">选择</el-button>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
        </el-form-item>
        <br>
        <el-form-item label="建筑物" style="margin-left: 45px">
          <el-input placeholder="请选择" style="width: 330px"/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-plus" size="small" circle/>
        </el-form-item>
        <br>
        <el-form-item label="楼层" style="margin-left: 60px">
          <el-input placeholder="请选择" style="width: 330px"/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-refresh-left" size="small" circle/>
          <el-button type="primary" style="margin-left: 10px" icon="el-icon-plus" size="small" circle/>
        </el-form-item>
        <br>
        <el-form-item label="地图位置 已标记位置信息" :required="true" label-width="200px">
          <el-button type="primary" style="margin-left: 10px" size="small">精确选择</el-button>
        </el-form-item>
        <br>
        <el-form-item label="详细地址" :required="true" label-width="100px">
          <el-input placeholder="请输入" style="width: 330px"/>
        </el-form-item>
        <br>
        <el-form-item label="安装点名称" :required="true" label-width="100px">
          <el-input placeholder="请输入安装点名称" style="width: 330px"/>
        </el-form-item>
        <br>
        <el-form-item label="接警联系人" :required="true" label-width="100px">
          <el-input placeholder="请选择其他联系人" style="width: 330px"/>
        </el-form-item>
        <br>
        <el-form-item label="住户资料" label-width="100px">
          <el-input placeholder="请选择联系人" style="width: 330px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAdd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--关联设备-->
    <el-dialog title="关联设备" :visible.sync="equipment" width="400px" append-to-body>

    </el-dialog>

    <!--解绑安装点-->
    <el-dialog title="操作提示" :visible.sync="unbindtitle" width="500px" append-to-body>
      <b>
        <div style="color: red;margin-left: 50px;font-size: 18px;">
          解绑后将无法收到报警和故障信息，产生的后果自行承担！主机解绑，所有子设备将全部解绑，请谨慎操作！确定解绑吗？
        </div>
      </b>
      <div>
        <b>
          <span class="cha"></span>
        </b>
      </div>
      <br><br>
      <el-button type="danger "  style="margin-left: 367px;margin-bottom: -10px;">确定删除</el-button>
    </el-dialog>

  </div>
</template>

<script>
import {buildingName} from "@/api/yunze/install/PlaceFloor";
import {listInstalLpoint,SelIMEI} from "@/api/yunze/install/installpoint";
import tools from "@/utils/yunze/tools";

export default {
  name: "index",
  data() {
    return {

      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      tools: tools,
      Name_result: [],//建筑物选择
      state2: '', //匹配建议
      restaurants: [], //匹配建议
      SelIMEIList:[], //查询IMEI

      equipment: false,//关联设备
      unbindtitle:false,//删除连接点
      selTime: '',//时间选择
      mainwidth: 24,//宽度
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 用户表格数据
      cardList: null,
      // 是否显示弹出层
      open: false,
      // 时间数组
      timetypeHis: [],
      //抄送状态
      CCstatus: [],
      // 抄送结果
      Cchis_result: [],
      // 查询条件
      typeOptionsCcUrl: [],
      // 表单参数
      form: {

      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 列信息
      columns: [
        {key: 1, label: `序号`, visible: true},
        {key: 2, label: `二维码`, visible: true},
        {key: 3, label: `安装点名称`, visible: true},
        {key: 4, label: `类型-型号`, visible: true},
        {key: 5, label: `建筑物`, visible: true},
        {key: 6, label: `楼层`, visible: true},
        {key: 7, label: `公司所属`, visible: true},
        {key: 8, label: `IMEI`, visible: true},
        {key: 9, label: `现场图片`, visible: true},
        {key: 10, label: `已关联数`, visible: true},
        {key: 11, label: `被关联数`, visible: true},
        {key: 12, label: `安装人`, visible: true},
        {key: 13, label: `安装时间`, visible: true},
      ],
      // 表单校验
      rules: {}
    };
  },

  created() {

    const id = this.$route.params.id;
    console.log(id)
    if (tools.Is_null(id)) {
      this.queryParams.name_id = Number(id)
    }


    //查询建筑物名称  Name_result
    if (window['Name_result'] != undefined && window['Name_result'] != null && window['Name_result'] != '') {
      this.Name_result = window['Name_result'];
    } else {
      buildingName().then(response => {
        let jsonobj = JSON.parse(tools.Decrypt(response));
        window['Name_result'] = jsonobj.Data;
        // console.log(jsonobj.Data)
        this.Name_result = window['Name_result'];
      });
    }

    //加载 查询条件
    if (window['typeOptionsCcUrl'] != undefined && window['typeOptionsCcUrl'] != null && window['typeOptionsCcUrl'] != '') {
      this.typeOptionsCcUrl = window['typeOptionsCcUrl'];
    } else {
      this.getDicts("yunze_typeOptionsCcUrl").then(response => {
        window['typeOptionsCcUrl'] = response.data;
        this.typeOptionsCcUrl = window['typeOptionsCcUrl'];
      });
    }

    //加载 抄送状态
    if (window['CCstatus'] != undefined && window['CCstatus'] != null && window['CCstatus'] != '') {
      this.CCstatus = window['CCstatus'];
    } else {
      this.getDicts("yz_cc_HisStatus").then(response => {
        window['CCstatus'] = response.data;
        this.CCstatus = window['CCstatus'];
      });
    }

    //加载 抄送结果
    if (window['Cchis_result'] != undefined && window['Cchis_result'] != null && window['Cchis_result'] != '') {
      this.Cchis_result = window['Cchis_result'];
    } else {
      this.getDicts("yz_cc_result").then(response => {
        window['Cchis_result'] = response.data;
        this.Cchis_result = window['Cchis_result'];
      });
    }

    //加载 时间赛选查询条件
    if (window['timetypeHis'] != undefined && window['timetypeHis'] != null && window['timetypeHis'] != '') {
      this.timetypeHis = window['timetypeHis'];
    } else {
      this.getDicts("yunze_CcHis_timetype").then(response => {
        window['timetypeHis'] = response.data;
        this.timetypeHis = window['timetypeHis'];
      });
    }


    //加载 执行日志导出类别
    if (window['ExecutionTask_OutType'] != undefined && window['ExecutionTask_OutType'] != null && window['ExecutionTask_OutType'] != '') {
      this.ExecutionTask_OutType = window['ExecutionTask_OutType'];
    } else {
      this.getDicts("yunze_ExecutionTask_OutType").then(response => {
        window['ExecutionTask_OutType'] = response.data;
        this.ExecutionTask_OutType = window['ExecutionTask_OutType'];
      });
    }


    this.getList();
  },
  methods: {

    /**查询IMEI号 进行加载*/
    querySearch(queryString, cb) {
        // console.log(queryString)
      let Pwd_Str = tools.encrypt(JSON.stringify(this.form));
      SelIMEI(Pwd_Str).then(response => {
        let jsonobj = JSON.parse(tools.Decrypt(response));
        // console.log(jsonobj);
        if (jsonobj.code == 200) {
          this.SelIMEIList = jsonobj.Data;
          let restaurants = this.SelIMEIList;
          let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
          cb(results);
        }
        // console.log(results)
        // 调用 callback 返回建议列表的数据
        // console.log(this.SelIMEIList)
      });
    },
    createFilter(queryString) {
      // console.log(queryString)
      return (restaurant) => {
        // console.log(restaurant)
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },

    /**关联设备*/
    Relation(){
      this.equipment = true;

    },

    /**解绑安装点*/
    Unbind(){
      this.unbindtitle = true;
    },

    /**新增确定按钮*/
    submitAdd() {

    },

    /**新增*/
    handleAdd() {
      this.open = true;
    },

    /**导出*/
    handleExport() {

    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.jobId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /*获取查询参数*/
    getParams() {

      if (this.$refs.dept != undefined & this.$refs.dept != null & this.$refs.dept != 'undefined') {
        //console.log(this.$refs.dept.getCheckedKeys());
        if (this.$refs.dept.getCheckedKeys().length > 0) {
          this.queryParams.agent_id = this.$refs.dept.getCheckedKeys();
        }
      }
      if (tools.Is_null(this.queryParams.timetype) && this.selTime != null) {
        //console.log(this.selTime);
        this.queryParams.staTime = this.selTime[0];
        this.queryParams.endTime = this.selTime[1];
      }


    },


    /** 查询执行日志列表 */
    getList() {
      this.loading = true;
      this.getParams();
      this.queryParams.page = 1;
      //console.log(this.queryParams);
      let Pwd_Str = tools.encrypt(JSON.stringify(this.queryParams));
      //console.log(Pwd_Str);
      listInstalLpoint(Pwd_Str).then(response => {
          let jsonobj = JSON.parse(tools.Decrypt(response));
          // console.log(jsonobj);
          if (jsonobj.code == 200) {
            this.cardList = jsonobj.Data.Data;
            this.total = jsonobj.Data.Pu.rowCount;
          } else {
            this.msgError("获取数据异常，请联系管理员！");
          }
          this.loading = false;
        }
      );
    },


    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {};
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.selTime = null;
      this.handleQuery();
    },
    loadAll() {
      return [
        // { "value": "869537059626342" },
        // { "value": "869537059626342"},
      ]
      return this.SelIMEIList ? this.SelIMEIList:[];
    },

    handleSelect(item) {
      //走这个
      console.log(item);
      this.form.dept_id = Number(item.dept_id);


    },
  },
  mounted() {
    this.restaurants = this.loadAll();
  }
};
</script>

<style scoped>


.cha::before,
.cha::after {
  content: "";
  position: absolute;
  /*方便进行定位*/
  height: 20px;
  width: 5px;
  top: 100px;
  right: 460px;
  /*设置top和right使图像在20*20框中居中*/
  background: #FF0000;
}

.cha::before {
  transform: rotate(45deg);
  /*进行旋转*/
}

.cha::after {
  transform: rotate(-45deg);
}



</style>

